﻿@model IEnumerable<NCLCustomerManagement.UI.Models.User.UserRequestModel>
@{
    Layout = "";
    var currentUser = Session[NCLCustomerManagement.Common.Constant.Constant.CURRENT_USER]
                       as NCLCustomerManagement.UI.Models.User.AuthenticatedUser;
    var username = currentUser.Username;
    var isAdmin = currentUser.IsAdmin;                       
}

<ul>
    @foreach (var item in Model)
    {
       
        <li id="@item.Id" class="@(isAdmin == true && item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Initial ? "new" : isAdmin == false && item.Viewed == false ? "new" : "")">
            <div class="row title">
                <div class="col-xs-10 title-text">
                    @if (!isAdmin)
                    {
                        <span>Yêu cầu của bạn lúc @string.Format("{0} ngày {1} {2}",
                        item.RequestedDate.ToLongTimeString(),
                        item.RequestedDate.ToString("dd/MM/yyyy"),
                            item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Approved ? "đã được phê duyệt" : "đã bị từ chối")
                        </span>
                    }
                    else
                    {
                        <span>@string.Format("{0} đã gửi cho bạn 1 yêu cầu vào lúc {1} ngày {2}",
                        item.Requester,
                        item.RequestedDate.ToLongTimeString(),
                        item.RequestedDate.Date == DateTime.Now.Date ? "hôm nay" : item.RequestedDate.ToString("dd/MM/yyyy"))

                        </span>
                    }

                </div>
                <div class="col-xs-2 detail-btn">
                    <a class="btn-xs expand-btn" title="Chi tiết" style="padding: 0px;"><span class="glyphicon glyphicon-expand"></span></a>
                </div>
            </div>
            <div class="detail">

                <div style="padding: 0px 0px 0px 20px; margin: 10px 0px 10px 0px;">
                    <div style="background-color: #ebf2f8; padding: 10px;">
                        Yêu cầu thay đổi thông tin khách hàng<b> @item.CurrentCustomer.CustomerName</b>, 
                        điện thoại:<b>@item.CurrentCustomer.Phone</b>
                        <p>
                            <div>Nội dung thay đổi:</div>
                            @(new MvcHtmlString(item.RequestContent))
                        </p>
                    </div>


                </div>
                <div class="btn-area">
                    @if (isAdmin)
                    {
                        if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Initial)
                        {
                        <a id="btnApprove" href="#" onclick="approveRequest(this);">Phê duyệt</a>                           
                        <a id="btnReject" href="#" onclick="rejectRequest(this);">Từ chối</a>                           
                        }
                        else
                        {
                            if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Rejected)
                            {  
                        <span>Đã từ chối 
                                            @(item.ApproveRejectDate.HasValue ? string.Format("lúc {0} ngày {1}",
                                                item.ApproveRejectDate.Value.ToLongTimeString(),
                                                item.ApproveRejectDate.Value.ToString("dd/MM/yyyy")) : ""
                                            )

                        </span>                          
                                
                            }
                            else
                            {
                                if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Approved)
                                {                            
                                    
                        <span>Đã phê duyệt
                                    @(item.ApproveRejectDate.HasValue ? string.Format("lúc {0} ngày {1}",
                                                        item.ApproveRejectDate.Value.ToLongTimeString(),
                                                        item.ApproveRejectDate.Value.ToString("dd/MM/yyyy")) : ""
                                    )
                        </span>
                                }
                                else
                                {
                        <a id="btnApprove" href="#" onclick="approveRequest(this);">Phê duyệt</a>
                                }
                            }
                        }


                    }
                    else
                    {
                        if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Approved)
                        { 
                        <a href="#" onclick="applyChange(this);">Áp dụng thay đổi</a>
                        }

                    }
                    @if (isAdmin)
                    {
                        if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Rejected)
                        {
                        <a href="#" id="btnDeleteRequest" onclick="deleteRequest(this);">Xóa</a>
                        }
                    }
                    else
                    {
                        if (item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Rejected
                            || item.Status == NCLCustomerManagement.DataTransferObjects.User.RequestStatus.Approved)
                        {
                        <a href="#" id="btnDeleteRequest" onclick="deleteRequest(this);">Xóa</a>
                        }
                    }

                </div>
            </div>
        </li>
    }
</ul>

<script>
    $(document).ready(function () {
        //$(".notify").mouseleave(function () {
        //    $(this).removeClass("show");
        //    $(this).addClass("hide");
        //});


        $(".notify").mCustomScrollbar({
            scrollButtons: {
                enable: true
            },
            advanced: {
                updateOnContentResize: true
            },
            theme: "dark-thin"
        });

        $(".detail-btn a").click(function () {
            var a = $(this).parent().parent().next().toggle();
            $(this).find("span.glyphicon-expand").toggleClass("glyphicon-collapse-down");

        });





        // Click expand button
        $(".expand-btn").click(function () {
            var msgId = $(this).parent().parent().parent().attr('id');
            var sender = $(this);

            GetCurrentLoginUser(function (data) {
                if (!data || !data.Username || data.Username === '')
                    window.location.href = '/User/Login';

                if (!data.IsAdmin) {
                    setMessageAsViewed(msgId);
                }
            });
            function setMessageAsViewed(id) {
                $.ajax({
                    url: "/Customer/SetMessageAsViewed/" + id,
                    data: {},
                    contentType: 'application/json',
                    dataType: "json",
                    type: "get",
                    success: function (data) {
                        $(".notify li[id=" + id + "]").removeClass("new");
                        refreshUnviewedMessageCount();
                    }
                });

            }


        });
    });

    // Approve request
    function approveRequest(a) {
        var msgId = $(a).parent().parent().parent().attr('id');

        $.ajax({
            url: "/Customer/ApproveRequest/" + msgId,
            data: {},
            contentType: 'application/json',
            dataType: "json",
            type: "get",
            success: function (data) {
                refreshMessage(msgId, true);
                refreshUnviewedMessageCount();
            }
        });
    }

    function refreshMessage(msgId, approved) {
        var item = $(".notify li[id=" + msgId + "]");
        $(item).removeClass("new");

        var btnToRemove, btnToAdd;
        if (approved) {
            btnToRemove = "#btnApprove";
            btnToAdd = "<span>Đã phê duyệt</span>";
        }
        else {
            btnToRemove = "#btnReject";
            btnToAdd = "<span>Đã từ chối</span><a href='#' id='btnDeleteRequest' onclick='deleteRequest(this);'>Xóa</a>";
        }
        $(item).find(btnToRemove).remove();
        $(item).find(".btn-area").html(btnToAdd);
    }
    // Reject request
    function rejectRequest(a) {
        var msgId = $(a).parent().parent().parent().attr('id');
        $.ajax({
            url: "/Customer/RejectRequest/" + msgId,
            data: {},
            contentType: 'application/json',
            dataType: "json",
            type: "get",
            success: function (data) {
                refreshMessage(msgId, false);
                refreshUnviewedMessageCount();
            }
        });
    }

    // Delete request
    function deleteRequest(a) {
        var msgId = $(a).parent().parent().parent().attr('id');
        $.ajax({
            url: "/Customer/DeleteRequest/" + msgId,
            data: {},
            contentType: 'application/json',
            dataType: "json",
            type: "get",
            success: function (data) {
                refreshMessageBoard(function () { });
                refreshUnviewedMessageCount();
            }
        });
    }

    function applyChange(a) {
        var msgId = $(a).parent().parent().parent().attr('id');
        $.ajax({
            url: "/Customer/ApplyChange/" + msgId,
            data: {},
            contentType: 'application/json',
            dataType: "json",
            type: "get",
            success: function (data) {
                if (data.Success) {
                    $(".notify").removeClass("show");
                    $(".notify").addClass("hide");
                }
                showMessage("#alertbox", data.Message, !data.Success);
            }
        });
    }
</script>
